import { getCurrentSong, getLyric } from "@/service/play";

import * as actionTypes from "./constants";

import { getRandomNumber } from "@/utils/format-utils";

import { parseLyric } from "@/utils/parse-lyc";

const changeCurrentSongAction = (currentSong) => ({
  type: actionTypes.CHANGE_CURRENT_SONG,
  currentSong,
});

const changLyricListAction = (lyricList) => ({
  type: actionTypes.CHANG_LYRIC_ACTION,
  lyricList,
});

export const changeCurrentIndexAction = (index) => ({
  type: actionTypes.CHANGE_CURRENT_INDEX,
  index,
});

export const changePlayListAction = (playList) => ({
  type: actionTypes.CHANGE_PLAYLIST_ACTION,
  playList,
});

export const changesequenceAction = (sequence) => ({
  type: actionTypes.CHANGE_SEQUENCE_ACTION,
  sequence,
});
//歌词相关
export const changeCurrentLyricIndexAction = (index) => ({
  type: actionTypes.CHANGE_LYRIC_INDEX,
  index,
});
export const changeCurrentIndexandSongAction = (tags) => {
  return (dispatch, getState) => {
    //播放列表
    const playList = getState().getIn(["palyer", "playList"]);
    //循环方式
    const sequence = getState().getIn(["palyer", "sequence"]);
    //当前的索引
    let currentSongIndex = getState().getIn(["palyer", "currentSongIndex"]);

    switch (sequence) {
      case 1:
        let randomIndex = getRandomNumber(playList.length);
        while (randomIndex === currentSongIndex) {
          randomIndex = getRandomNumber(playList.length);
        }
        currentSongIndex = randomIndex;
        break;
      default: // 顺序播放
        currentSongIndex += tags;
        if (currentSongIndex >= playList.length) currentSongIndex = 0;
        if (currentSongIndex < 0) currentSongIndex = playList.length - 1;
    }
    const currentSong = playList[currentSongIndex];
    dispatch(changeCurrentSongAction(currentSong));
    dispatch(changeCurrentIndexAction(currentSongIndex));
    // 请求歌词
    dispatch(getSongLyricAction(currentSong.id));
  };
};

export const getSongDetailAction = (ids) => {
  return (dispatch, getState) => {
    //读取歌曲列表 查找是否已经有该歌曲
    const playList = getState().getIn(["palyer", "playList"]);
    const songIndex = playList.findIndex((song) => song.id === ids);
    let song = null;

    if (songIndex !== -1) {
      //有这首歌
      //改变歌曲的索引
      dispatch(changeCurrentIndexAction(songIndex));
      song = playList[songIndex];
      //改变当前播放的歌曲
      dispatch(changeCurrentSongAction(playList[songIndex]));
      //获取歌词
      dispatch(getSongLyricAction(song.id));
    } else {
      //没有这首歌 请求获取该歌曲详情
      getCurrentSong(ids).then((res) => {
        const song = res.songs && res.songs[0];

        if (!song) return;

        const newPlayList = [...playList];

        newPlayList.push(song);

        dispatch(changePlayListAction(newPlayList));

        dispatch(changeCurrentIndexAction(newPlayList.length - 1));

        dispatch(changeCurrentSongAction(res.songs[0]));
        //获取歌词
        dispatch(getSongLyricAction(song.id));
      });
    }
  };
};

export const getSongLyricAction = (id) => {
  return (dispatch) => {
    getLyric(id).then((res) => {
      const lyric = res.lrc.lyric;
      const lyricList = parseLyric(lyric);
      dispatch(changLyricListAction(lyricList));
    });
  };
};
